package com.example.imchat.controller.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.imchat.R;
import com.example.imchat.model.bean.InvivationInfo;
import com.example.imchat.model.bean.UserInfo;

import java.util.ArrayList;
import java.util.List;

/*
* 邀请信息界面适配器
* */
public class InviteAdapter extends BaseAdapter {
    private Context mContext;
    private List<InvivationInfo> mInvationInfos = new ArrayList<>();
    private OnInviteListener mOnInviteListener;

    public InviteAdapter(Context context,OnInviteListener OnInviteListener) {
        mContext = context;
        mOnInviteListener = OnInviteListener;
    }

    //刷新数据的方法
    public void refresh(List<InvivationInfo> invationInfos){
        if (mInvationInfos !=null && invationInfos.size() >= 0){
            mInvationInfos.clear();
            mInvationInfos.addAll(invationInfos);

            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
       return mInvationInfos == null? 0 :mInvationInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInvationInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
       return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //获取或创建一个ViewHoldler
        ViewHoldler viewHoldler = null;
        if (null == convertView){
            viewHoldler = new ViewHoldler();
            convertView = View.inflate(mContext, R.layout.item_invite,null);
            viewHoldler.name   = (TextView)convertView.findViewById(R.id.tv_invite_name);
            viewHoldler.reason =(TextView) convertView.findViewById(R.id.tv_invite_reason);
            viewHoldler.accept = (Button) convertView.findViewById(R.id.bt_invite_accept);
            viewHoldler.reject = (Button) convertView.findViewById(R.id.bt_invite_reject);

            convertView.setTag(viewHoldler);
        }else {
            viewHoldler = (ViewHoldler)convertView.getTag();

        }
        //获取当前item数据
        InvivationInfo invationInfo =mInvationInfos.get(position);
        //显示当前item数据
        UserInfo user  =invationInfo.getUser();

        /*
        * 联系人的邀请展示
        *
        * */
        if(null != user){
            //名称展示
            viewHoldler.name.setText(invationInfo.getUser().getName());

            viewHoldler.accept.setVisibility(View.GONE);
            viewHoldler.reject.setVisibility(View.GONE);

            //原因
            if (invationInfo.getStatus() == InvivationInfo.InvitationStatus.NEW_INVITE){ //新的邀请
                viewHoldler.accept.setVisibility(View.VISIBLE);
                viewHoldler.reject.setVisibility(View.VISIBLE);
                viewHoldler.reason.setText("交个朋友吧");
            }else if (invationInfo.getStatus() == InvivationInfo.InvitationStatus.INVITE_ACCEPT){//接受邀请
                viewHoldler.reason.setText("接受邀请");
            }else if (invationInfo.getStatus() == InvivationInfo.InvitationStatus.INVITE_ACCEPT_BY_PEER){//邀请被接受
                viewHoldler.reason.setText("邀请被接受");
            }else if (invationInfo.getStatus() == InvivationInfo.InvitationStatus.INVITE_DECLINED){  //邀请被拒绝
                viewHoldler.reason.setText("邀请被拒绝");

            }

            //接受按钮的处理
            viewHoldler.accept.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onAccept(invationInfo);
                }
            });
            //拒绝按钮的处理
            viewHoldler.reject.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mOnInviteListener.onReject(invationInfo);
                }
            });

            /*
            *          群组的邀请信息展示
            * */
        }else{  //群组邀请
          //显示名称
            viewHoldler.name.setText(invationInfo.getGroup().getInvatePerson());
            viewHoldler.accept.setVisibility(View.GONE);
            viewHoldler.reject.setVisibility(View.GONE);
            //显示原因
            switch (invationInfo.getStatus()){
                //你的群申请已经被接受
                case GROUP_APPLICATION_ACCEPTED:
                    viewHoldler.reason.setText("你的群申请已被接受");
                    break;
                //你的群邀请已经被接受
                case GROUP_INVITE_ACCEPTED:
                    viewHoldler.reason.setText("你的群邀请已被接受");
                    break;
                //你的群申请已经被拒绝
                case GROUP_APPLICATION_DECLINED:
                    viewHoldler.reason.setText("你的群申请已被拒绝");
                    break;
                    //你拒绝了群邀请
                case GROUP_REJECT_INVITE:
                    viewHoldler.reason.setText("你拒绝了群邀请");
                    break;
                //你拒绝了群申请
                case GROUP_REJECT_APPLICATION:
                    viewHoldler.reason.setText("你拒绝了群申请");
                    break;
                //你的群邀请已经被拒绝
                case GROUP_INVITE_DECLINED:
                    viewHoldler.reason.setText("你的群邀请已被拒绝");
                    break;
                //您收到群邀请
                case NEW_GROUP_INVITE:
                    viewHoldler.accept.setVisibility(View.VISIBLE);
                    viewHoldler.reject.setVisibility(View.VISIBLE);

                    viewHoldler.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                               mOnInviteListener.onInviteAccept(invationInfo);
                        }
                    });

                    viewHoldler.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onInviteReject(invationInfo);

                        }
                    });

                    viewHoldler.reason.setText("您收到了群邀请");
                    break;

                //您收到群申请
                case NEW_GROUP_APPLICATION:
                    viewHoldler.accept.setVisibility(View.VISIBLE);
                    viewHoldler.reject.setVisibility(View.VISIBLE);
                    viewHoldler.accept.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationAccept(invationInfo);

                        }
                    });

                    viewHoldler.reject.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mOnInviteListener.onApplicationReject(invationInfo);

                        }
                    });
                    viewHoldler.reason.setText("您收到群申请");
                    break;
                //您接受了群邀请
                case GROUP_ACCEPT_INVITE:
                    viewHoldler.reason.setText("您接受了群邀请");
                    break;
                //您批准了群加入
                case GROUP_ACCEPT_APPLICATION:
                    viewHoldler.reason.setText("您批准了群加入");
                    break;
                case AUTO_NEW_GROUP_INVITE:
                    break;


            }


        }


        //返回view
        return  convertView;

    }
    private class ViewHoldler{
        private TextView name;
        private TextView reason;

        private Button accept;
        private Button reject;
    }

    public interface  OnInviteListener{

        //联系人接受按钮的点击事件
        void onAccept(InvivationInfo invationInfo);

        //联系人拒绝按钮的点击事件
        void onReject(InvivationInfo invationInfo);

        //接受邀请按钮处理
        void onInviteAccept(InvivationInfo invationInfo);

        //拒绝邀请按钮处理
        void onInviteReject(InvivationInfo invationInfo);

        //接受申请按钮处理
        void onApplicationAccept(InvivationInfo invationInfo);

        //拒绝申请按钮处理
        void onApplicationReject(InvivationInfo invationInfo);
    }
}
